﻿<%@ WebHandler Language="C#" Class="AHMIDEAF02" %>

using System;
using System.Web;
using System.Linq;
using System.Collections.Generic;

public class AHMIDEAF02 : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        AjaxResponse axresp = new AjaxResponse();
        string user = AHMIDEAFunctions.ContextUser(context);

        // Field Names
        string f_dispname = "txtdispname";
        string f_email = "txtemail";
        string f_action = "action";

        // Field valuess
        string v_action = AHMIDEAFunctions.ContextPostData(context, f_action);
        string v_dispname = AHMIDEAFunctions.ContextPostData(context, f_dispname);
        string v_email = AHMIDEAFunctions.ContextPostData(context, f_email);

        if (!string.IsNullOrEmpty(v_action) && ( v_action.ToUpper() == "UPLOAD"))
        {
            // handle file upload
            if (context.Request.Files.Count > 0)
            {
                HttpPostedFile file = context.Request.Files[0];

                if (file.ContentLength <= 0)
                {
                    axresp.Status = "0";
                    axresp.Message = "File is empty. Please upload another file.";
                }
                else if (file.ContentLength > 102400)
                {
                    axresp.Status = "0";
                    axresp.Message = "File is limited to 100kb only.";
                }
                else
                { 
                    List<string> allowedExts = new List<string>(new string[]{".gif",".jpg",".png"});
                    String ext = System.IO.Path.GetExtension(file.FileName);
                    if (!allowedExts.Contains(ext))
                    {
                        axresp.Status = "0";
                        axresp.Message = "Sorry, you are only allowed to upload gif, jpg or png image. Extension '"+ext+"' is not allowed";
                    }
                    else { 
                        // handle save image
                        byte[] img = new byte[file.ContentLength];
                        file.InputStream.Read(img, 0, file.ContentLength);

                        using (DC_AHMIDEADataContext dc = new DC_AHMIDEADataContext())
                        {
                            var i = dc.AHMIDEA_MSTPROFILEs.FirstOrDefault(p => p.vuserid==user);
                            if (i != null) {
                                i.bimage = img;
                                i.vtype = file.ContentType;
                                dc.SubmitChanges();
                                axresp.Status = "1";
                                axresp.Message = "Your profile picture has been sucessfully updated. Please relogin to take effect.";
                            }
                        }
                    }
                }
                
                
            }
            else {
                axresp.Status = "0";
                axresp.Message = "No file received.";
            }
            
        }
        else
        {
            // handle save data

            // validations
            FormValidation validation = new FormValidation();
            validation.RegisterField(f_dispname, "Display Name", v_dispname, "Required,LimitLength", 50);
            validation.RegisterField(f_email, "Email", v_email, "Required,LimitLength", 100);
            validation.ValidateFields();

            if (validation.IsValid)
            {
                string usr = AHMIDEAFunctions.ContextUser(context);
                using (DC_AHMIDEADataContext dc = new DC_AHMIDEADataContext())
                {
                    var p = dc.AHMIDEA_MSTPROFILEs.FirstOrDefault(i => i.vuserid == usr);
                    if (p != null)
                    {
                        p.vdisplayname = v_dispname;
                        p.vemail = v_email;
                        dc.SubmitChanges();
                        axresp.SetStatus(validation, "Your profile is successfully updated. Please relogin to take effect.");
                    }
                    else
                    {
                        axresp.Status = "0";
                        axresp.Message = "Cannot find your profile.";
                    }
                }
            }
            else
            {
                axresp.SetStatus(validation);
            }
        }

        context.Response.Write(axresp.GenerateJSON());
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}